/**
  *****************************************************************************
  * @file RBM350 瑞宝1Torr及0.1Torr真空计驱动（同inflcon CDG025D系列）
  * @brief 
  * @author Evan
  * @version V1.0.0
  * @date 2023/10/19
  *****************************************************************************
  */
 /* Includes -----------------------------------------------------------------*/
#include "dev_rbm350.h"
#include "drv_f4_gpio.h"
#include "drv_f4_usart.h"
#include "drv_f4_delay_168MHz.h"
#include "system.h"
#include "device_table.h"
#include "FreeRTOS.h"
#include "task.h"
#include "func_DownMode.h"

/* Private defines ----------------------------------------------------------*/
#define USE_RBM350_DEBUG 1

/* Private Struct -----------------------------------------------------------*/

/* Private enum -------------------------------------------------------------*/

/* Private Variable ---------------------------------------------------------*/
#if USE_RBM350_DEBUG
static uint8_t tx_buf_db[64];
static uint8_t tx_len_db = 0;
static uint8_t rx_buf_db[64];
static uint8_t rx_len_db = 0;
#endif

//Pointer

//Array
uint8_t rbm350_addr[2] = {0x30,0x31}; //RBM350地址
uint8_t rbm350_cmd = RBMSET_MODE;
//uint8_t rbm350_en = 0;
//Const
rbm350val_t rbm350val;

/* Private function prototypes ----------------------------------------------*/

drv_usart_t rbm350_com = //rbm350压力传感器串口(232)
{
    .Drv_Num = "USART2",
    .Tx_Pin = "A2",
    .Rx_Pin = "A3",
    .BaudRate = 9600,
    .WordLength = USART_WORDLENGTH_8BIT,
    .StopBits = USART_STOPBITS_1BIT,
    .Parity = USART_PARITY_NO,
    .Idle_CallBack = RBM350ComCallBack,
    .Rx_Priority = 5,
};

func_DownMode_t rbm350_fun = //rbm350压力传感器下行设备管理
{
    .outTime = 20,    //超时时间
    .SendCnt = 0,       //重发次数
    .IdleDelay = 100,   //总线空闲时间
    .send_data = RBM350Com_send_data,
};


/*****************************************************************************
* @brief rbm350 发送接口函数
* ex:
* @par 
* None
* @retval 
*****************************************************************************/
uint32_t RBM350Com_send_data(uint8_t *data, uint32_t len)
{
    rbm350_com.Write(&rbm350_com, data, len);
	vTaskDelay(pdMS_TO_TICKS(2));

	return 0;
}

/*****************************************************************************
* @brief  rbm350 232串口接收回调函数
* ex:
* @par 
* None
* @retval 
*****************************************************************************/
static void RBM350ComCallBack(uint8_t *data, uint32_t len)
{
#if USE_RBM350_DEBUG	
	memcpy(rx_buf_db, data, len);
#endif
	rbm350_fun.read_callback(&rbm350_fun, data, len);
}

/*****************************************************************************
* @brief  获取RBM350压力传感器处理的工作任务状态
* ex:
* @par 
* None
* @retval 
*****************************************************************************/
static DOWN_TASK_STATE rbm350_task_st(void *fun)
{
    if (fun == NULL)
        return Task_IsOver;
    else
        return Task_IsOk;
}

/*****************************************************************************
* @brief  RBM350压力传感器任务接收数据处理
* ex:
* @par 
* None
* @retval 
*****************************************************************************/
static int32_t rbm350_task_recv(void *fun, uint8_t *pBuf, int32_t len)
{
    if (fun == NULL)
        return -1;

    if (pBuf == NULL)
    {
        rbm350val.status = 0;
		
        return 0;
    }
    else
        rbm350val.status = 1;  

    RBM350MastDecode(pBuf, len, 0);
	
//    pressuresensor_log_print("RX:", (char *)pBuf, (int)len);
    return 0;
}

/*****************************************************************************
* @brief  RBM350压力传感器任务发送数据处理
* ex:
* @par 
* None
* @retval 
*****************************************************************************/
static int32_t rbm350_task_send(void *fun, uint8_t *pBuf)
{
    int32_t res;
    if (fun == NULL || pBuf == NULL)
        return -1;

    res = RBM350AssembleSendCom(pBuf);
	
#if USE_RBM350_DEBUG	
	memcpy(tx_buf_db, pBuf, 5);
#endif 
	
//    pressuresensor_log_print("TX:", (char *)pBuf, (int)res);
    return res;
}

/*******************************************************************************
 * @brief    RBM350组装发送命令
 *
 * @param 
 * @param                       
 * @param Cali date = 8936A90A=2,302,060,810
 * @param
 *
 * @return
*******************************************************************************/
int RBM350AssembleSendCom(uint8_t *p_dst)
{
    uint16_t len = 0;
    if(p_dst == NULL)
        return -1;
    
    p_dst[len++] = 0x03;//数据长度
    switch(rbm350_cmd)
    {
        case RBMSET_MODE:
            p_dst[len++] = WRITECMD;
            p_dst[len++] = 0x00;
            p_dst[len++] = 0x01;//设置为单次读取模式
            p_dst[len++] = WRITECMD + 0x00 + 0x01;
            rbm350_cmd =  RBMREAD_SW;
            break;
        
        case RBMSET_UNIT:
            p_dst[len++] = WRITECMD;
            p_dst[len++] = 0x01;
            p_dst[len++] = 0x01;//设置为Torr
            p_dst[len++] = WRITECMD + 0x01 + 0x01;
            break;
            
        case RBMREAD_SW:
            p_dst[len++] = READCMD;
            p_dst[len++] = 0x10;
            p_dst[len++] = 0x00;//无意义字节
            p_dst[len++] = READCMD + 0x10 + 0x00;
            break;
                
        case RBM_RST: // Power reset.
            p_dst[len++] = SPECIALCMD;//R
            p_dst[len++] = 0x00;
            p_dst[len++] = 0x00;
            p_dst[len++] = SPECIALCMD + 0x00 + 0x00;
            break;
		
//		case RBM_EXT_ERR:
//			p_dst[len++] = READCMD;
//            p_dst[len++] = 0x55;
//            p_dst[len++] = 0x00;//无意义字节
//            p_dst[len++] = READCMD + 0x54 + 0x00;
//            break;
        
        default:
            return -2;
     }       
        
    
    return len;
}



/*******************************************************************************
 * @brief    接收命令解析
 *
 * @param    p_data:接收的数据
 * @param   len：接收数据长度
 * @param uint:Torr
 * @param
 *
 * @return
*******************************************************************************/
int RBM350MastDecode(uint8_t *p_data, uint8_t len, uint16_t offset)
{
    float factor_a = 0;
    float factor_b = 0;
    float FSR_Exponent = 1.0;
    float FSR_Mantissa = 1.0;
    int16_t pre_value = 0;

    if((len == 9) && (p_data[0] == 7)) // valid frame
    {
       //if(p_data[CDG_BYTE_ERR] == 0)//若无错误标志
		if(1)//若无错误标志
        {     
            if((p_data[CDG_BYTE_SENTYPE] & 0xF) == 0x00)
                FSR_Exponent = 0.001;
            else if((p_data[CDG_BYTE_SENTYPE] & 0xF) == 0x01)
                FSR_Exponent = 0.01;
            else if((p_data[CDG_BYTE_SENTYPE] & 0xF) == 0x02)
                FSR_Exponent = 0.1;
            else if((p_data[CDG_BYTE_SENTYPE] & 0xF) == 0x03)
                FSR_Exponent = 1.0;
            else if((p_data[CDG_BYTE_SENTYPE] & 0xF) == 0x04)
                FSR_Exponent = 10.0;
            else if((p_data[CDG_BYTE_SENTYPE] & 0xF) == 0x05)
                FSR_Exponent = 100.0;
            else if((p_data[CDG_BYTE_SENTYPE] & 0xF) == 0x06)
                FSR_Exponent = 1000.0;
            else if((p_data[CDG_BYTE_SENTYPE] & 0xF) == 0x07)
                FSR_Exponent = 10000.0;
            
            if((p_data[CDG_BYTE_SENTYPE] & 0xF0) == 0x00)
                FSR_Mantissa = 1.0;
            else if((p_data[CDG_BYTE_SENTYPE] & 0xF0) == 0x10)
                FSR_Mantissa = 1.1;
            else if((p_data[CDG_BYTE_SENTYPE] & 0xF0) == 0x20)
                FSR_Mantissa = 2.0;
            else if((p_data[CDG_BYTE_SENTYPE] & 0xF0) == 0x30)
                FSR_Mantissa = 2.5;
            else if((p_data[CDG_BYTE_SENTYPE] & 0xF0) == 0x40)
                FSR_Mantissa = 5.0;
            else
                return -2;
           
            //计算参数a、b
            if(((p_data[CDG_BYTE_STA] & 0x30) == STA_UNIT_MBAR) && (FSR_Mantissa == 1.1f))
            {
                factor_a = 13332.0f;
                factor_b = 26400.0f;
            }
            else if(((p_data[CDG_BYTE_STA] & 0x30) == STA_UNIT_MBAR) && (FSR_Mantissa != 1.1f))
            {
                factor_a = 1.3332f;
                factor_b = 24000.0f;
            }
            else if(((p_data[CDG_BYTE_STA] & 0x30) == STA_UNIT_TORR) && (FSR_Mantissa != 1.1f))
            {
                factor_a = 1.0f;
                factor_b = 32000.0f;
            }
            else if(((p_data[CDG_BYTE_STA] & 0x30) == STA_UNIT_PA) && (FSR_Mantissa != 1.1f))
            {
                factor_a = 133.32f;
                factor_b = 24000.0f;
            }
            else
                return -3;
            
            //计算压力值            
            pre_value =  (int16_t)p_data[4] << 8 | p_data[5];
            
            rbm350val.P_sensor = (double)(pre_value * factor_a / factor_b * FSR_Mantissa * FSR_Exponent); 
			//rbm350val.P_sensor = (double)(pre_value * factor_a / factor_b * FSR_Mantissa * pow(10,FSR_Exponent) ); 
        }
        else{
			
			//rbm350_cmd = RBM_EXT_ERR;
			
			rbm350val.status = p_data[CDG_BYTE_ERR];
            return p_data[CDG_BYTE_ERR];//若有错误标志则返回错误码
		}
        
        return -1;
    }
    
    return 0;

}

/***********************************************************************************
 * @brief 压力传感器初始化		
 * ex:
 * @par
 * None
 * @retval void None
 **********************************************************************************/
void RBM350_init(void)
{

    if (Drv_Usart_Init(&rbm350_com) < 0)
    {
        while (1); 
    }

    if (Func_DownMode_Init(&rbm350_fun) < 0)
    {
        while (1); 
    }

//	set_modbus_page(&ps_page, P_sensor_num);
//    //set_modbus_page(&ps_page[0], P_sensor_num);  //AB串口共用一个表
	
    rbm350_fun.CommTaskApply(&rbm350_fun, ComType_Cycle, rbm350_task_st,
                          rbm350_task_send, rbm350_task_recv);
}

/***********************************************************************************
 * @brief 压力传感器轮训函数	
 * ex:
 * @par
 * None
 * @retval void None
 **********************************************************************************/
void RBM350_handler(uint16_t timebase)
{
    rbm350_fun.DownMode_task(&rbm350_fun, timebase);
	
	//SoftDelay_ms(100);
	//将压力缓存提供接口可读多个值
}
